package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.LawsType;
import com.neuedu.service.LawsTypeService;

@Controller
@RequestMapping(value="/admin/lawsType")
public class LawsTypeAction {
	
	@Autowired
	private LawsTypeService LawsTypeService;
	
	@RequestMapping(value = "/findList")
	public void findList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<LawsType> list = LawsTypeService.findList();
		
		String str = JSONObject.toJSONString(list);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request,HttpServletResponse response)throws Exception{
		/*
		 * 从客户端接收easyui发送过来的分页控制参数
		 * page指查询第几页，rows指每页查询多少条
		 * 这两个参数由easyui进行计算并发送到服务端
		 */
		int page=Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> pageMap=new HashMap<String, Object>();
		pageMap.put("startPage", (page-1)*rows); //从第几条数据开始查询
		pageMap.put("endPage", rows);	//每页查询多少条记录
		//进行分页查询
		System.out.println("11111111111111111111");
		List<LawsType> u=LawsTypeService.findAll(pageMap);
		System.out.println("22222222222222222");
		//进行查询记录总数，把记录总数发送给easy UI，用于分页计算
		int total=LawsTypeService.countLawsType();
		
		//前后端分离的程序结构，大部分会使用json数据结构进行数据传递（也有使用xml传递的）
		map.put("rows", u);
		map.put("total", total);
		
		//使用阿里巴巴的fastjon把java对象转换成json字符串
		String str=JSONObject.toJSONString(map);
		/*
		 * 使用servlet技术，把json数据字符串从服务器发送到
		 * 浏览器客户端（easy ui，由easy ui进行数据显示
		 */
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="addLawsType")
	public void addLawsType(LawsType LawsType,HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=LawsTypeService.addLawsType(LawsType);
		System.out.println(res);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		System.out.println("33333333333");
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="updateLawsType")
	public void updateUsers(LawsType LawsType,HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=LawsTypeService.updateLawsType(LawsType);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="deleteLawsType")
	public void deleteUsers(LawsType LawsType,HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=LawsTypeService.deleteLawsType(LawsType.getSortNo());
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
}
